<?php
namespace app\index\controller;

use mail\Mail;
use think\Db;
use think\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use think\Request;

class IndexController extends BaseController
{
//
    public function _initialize(Request $request)
    {
        $this->request = $request;
    }
    public function index()
    {
        $data = db('content')->paginate(3);
        $this->assign('data',$data);
        return view();
    }
    public function getList(){
        $key =$this->request->param('key');
        $page = $this->request->param('page');
        $limit = $this->request->param('limit');
        if (isset($key)&&!empty($key)){
            $where[] = ['username','like','%'.$key.'%'];
            $where2[] = ['content','like','%'.$key.'%'];
            $res = db('content')
                ->whereOr($where)
                ->whereOr($where2)
                ->page($page)
                ->limit($limit)
                ->select();
            db('test')->insert(['test'=>json_encode($res)]);
            $total = db('content')
                ->whereOr($where)
                ->whereOr($where2)
                ->count('id');
            return json(['code'=>1,'data'=>$res,'total'=>$total]);
        }
        $data = db('content')->page($page)->limit($limit)->select();
        $total = db('content')->count('id');
        return json(['code'=>1,'data'=>$data,'total'=>$total]);
    }
}
